﻿using System;
using System.Collections.Generic;
using System.Linq;
using MkLink;

namespace SymbolicLinkManageCore.Exceptions
{
    public static class ExUtilityTools
    {
        public static IEnumerable<TSource> GetDuplicates<TSource,TSelect>(this IEnumerable<TSource> Soutce,Func<TSource, TSelect> keySelector)
        {
            HashSet<TSelect> Set = new();
            foreach (TSource item in Soutce) 
            {
                if (!Set.Add(keySelector(item)))
                    yield return item;
            }
        }
        public static IEnumerable<RecordData> GetRecordDataDistinct(this IEnumerable<RecordData> Source)
        {
            IEnumerable<IGrouping<bool, RecordData>>? Group = Source.GroupBy(p => p.linkType == LinkType.HardLink);
            HashSet<string> Set = new(StringComparer.OrdinalIgnoreCase);

            foreach (RecordData item in Group.FirstOrDefault(p => !p.Key) ?? new RecordData[0].AsEnumerable())
            {
                if (Set.Add(item.LinkPointPath))
                    yield return item;
            }    
            Set.Clear();
            foreach (RecordData item in Group.FirstOrDefault(p => p.Key) ?? new RecordData[0].AsEnumerable())
            {
                if(Set.Add(item.CompactHardLinkGroupString))
                    yield return item;
            }
        }
        public static IEnumerable<RecordData> GetHardLinkGroupDuplicates(this IEnumerable<RecordData> Source)
        {
            Source = Source.GroupBy(p => p.linkType == LinkType.HardLink)?.FirstOrDefault(p => p.Key) ?? new RecordData[0].AsEnumerable();
            if (Source.Count() < 1)
                yield break;
            List<RecordData> List = new();
            List.Add(Source.First());
            Source = Source.Skip(1);
            bool isHis = false;
            foreach (RecordData item in Source)
            {
                isHis = false;
                foreach (RecordData ListItem in List)
                {
                    if (RecordData.EqualsHardLinkGroupCompletelyIdentical(ListItem.HardLinkGroup ?? [], item.HardLinkGroup ?? []))
                    {
                        isHis = true;
                        yield return item;
                        break;
                    }
                }
                if (!isHis)
                    List.Add(item);
            }
        }
        public static IEnumerable<(RecordData Source, RecordData Reference)> GetHardLinkIntersection(this IEnumerable<RecordData> Sources, IEnumerable<RecordData> References)
        {
            foreach (RecordData Source in Sources)
            {
                foreach (RecordData Reference in References)
                {
                    if(RecordData.ContainsFromHardLinkGroup(Source, Reference))
                        yield return (Source, Reference);
                    
                }
            }
        }

        public static bool IsSupportLinkType(this LinkType vaue) => (vaue & LinkType.NotSupport) == LinkType.Invalid;
    }
}
